package com.buss.base.controller;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.buss.base.model.Branch;
import com.buss.base.service.BranchService;
import com.buss.core.controller.BaseController;
import com.buss.core.dic.RecordStatus;
import com.buss.core.exception.WebException;
import com.buss.core.persistence.OrderBy;
import com.buss.core.persistence.Pagination;
import com.buss.core.persistence.ResultList;
import com.buss.core.sysparam.SysStaticParam;

@Controller
@RequestMapping("/admin/branch")
public class BranchController extends BaseController {

	@Autowired
	private BranchService branchService;
	
	@Override
	@RequestMapping(value="/page")
	public ModelAndView listPage(String menuid) {
		ModelAndView view = new ModelAndView("branch/listpage");
		view.addObject(SysStaticParam._MENUID_NAME, menuid);
		return view;
	}

	@Override
	@RequestMapping("/{type}")
	public ModelAndView editPage(@PathVariable String type, Long id) {
		ModelAndView view = new ModelAndView("branch/edit");
		Branch branch = new Branch();
		if(EDIT_PAGE.equals(type)){
			branch = branchService.loadBranch(id);
		}else{
			branch.setStatus(RecordStatus.normal);
		}
		view.addObject("branch",branch);
		return view;
	}

	@Override
	@RequestMapping("/delete")
	@ResponseBody
	public Map<String, Object> delete(Long[] ids)  throws WebException{
		branchService.deleteBranch(ids);
		return SysStaticParam.resultMessage("删除店铺信息成功!");
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public Map<String,Object> add( Branch branch,HttpServletRequest request){
		branch.setAddDate(new Date());
		branch.setAddUser(userId(request));
		branch.setCompanyId(getCompanyId(request));
		branchService.saveBranch(branch);		
		return SysStaticParam.resultMessage("新增店铺信息成功!");
	}

	@RequestMapping("/edit")
	@ResponseBody
	public Map<String,Object> edit( Branch branch,HttpServletRequest request){
		branch.setCompanyId(getCompanyId(request));
		branchService.editBranch(branch);
		if(getBranch(request).getId() == branch.getId()){
			request.getSession().setAttribute(SysStaticParam.SESSION_BRANCH, branch);
		}
		return SysStaticParam.resultMessage("修改店铺信息成功!");
	}
	
	@RequestMapping("/updatestatus/{status}")
	@ResponseBody
	public Map<String,Object> updateStatus(Long[] ids,@PathVariable RecordStatus status){
		branchService.updateStatus(ids, status);
		return SysStaticParam.resultMessage("修改状态成功!");
	}

	@RequestMapping("/list")
	@ResponseBody
	public Map<String,Object> list(String name,String pinyin,
			String orderField,String orderType,RecordStatus status,  Pagination page,
			HttpServletRequest request){
		OrderBy orderBy = new OrderBy();
		orderBy.add(orderField, orderType);
		ResultList<Branch> rs = branchService.queryBranch(getCompanyId(request), name, pinyin,status, page, orderBy);
		Map<String, Object> map = SysStaticParam.resultMessage("获取店铺信息成功!");
		map.put(SysStaticParam._RESULT, rs);
		return map;
	}
	
	@RequestMapping("/listbranchbyrole/{roleId}")
	@ResponseBody
	public Map<String,Object> queryBranchByRole(@PathVariable Long roleId,  Pagination page){
		Long[] roleIds = new Long[]{roleId};
		ResultList<Branch> rs = branchService.queryBranchByRole(roleIds,null, page);
		Map<String,Object> map = SysStaticParam.resultMessage("获取店铺信息成功!");
		map.put(SysStaticParam._RESULT, rs);
		return map;
	}
	
	@RequestMapping("/listbranchbyroles")
	@ResponseBody
	public Map<String,Object> queryBranchByRoles(RecordStatus status, Pagination page,HttpServletRequest request){
		ResultList<Branch> rs = branchService.queryBranchByRole(getUserRoleIds(request), status, page);
		Map<String, Object> map = SysStaticParam.resultMessage("获取店铺信息成功!");
		map.put(SysStaticParam._RESULT, rs);
		return map;
	}
	
	@RequestMapping("/savebranchrole/{roleId}")
	@ResponseBody
	public Map<String,Object> saveBranchRole(@PathVariable Long roleId,Long[] branchIds){
		branchService.saveBranchRole(roleId, branchIds);
		return SysStaticParam.resultMessage("设置店铺权限成功!");
	}
	
	@RequestMapping("/changebrach/{branchId}")
	@ResponseBody
	public Map<String,Object> loadCurentBranch(@PathVariable Long branchId,HttpServletRequest request){
		Branch branch = branchService.loadBranch(branchId);
		request.getSession().setAttribute(SysStaticParam.SESSION_BRANCH, branch);
		Map<String, Object> map = SysStaticParam.resultMessage("切换当前店铺信息成功!");
		map.put("branch", branch);
		return map;
	}
	
	@RequestMapping("/cancelbranchrole/{roleId}")
	@ResponseBody
	public Map<String,Object> saveCancelBranch(@PathVariable Long roleId,Long[] branchIds){
		branchService.saveCancelBranch(roleId, branchIds);
		return SysStaticParam.resultMessage("取消店铺权限成功!");
	}
}
